package 并发;

public class _01_循环打印 {

    private int state = 0;
    private int count = 0;

    public synchronized void printA() throws InterruptedException {
        while (count < 10) {
            if (state % 3 == 0) {
                System.out.print("A");
                state++;
                count++;
                notifyAll();
            } else {
                wait();
            }
        }
    }

    public synchronized void printB() throws InterruptedException {
        while (count < 10) {
            if (state % 3 == 1) {
                System.out.print("B");
                state++;
                count++;
                notifyAll();
            } else {
                wait();
            }
        }
    }

    public synchronized void printC() throws InterruptedException {
        while (count < 10) {
            if (state % 3 == 2) {
                System.out.print("C");
                state++;
                count++;
                notifyAll();
            } else {
                wait();
            }
        }
    }
}
